//
//  TodoViewController.swift
//  exam2
//
//  Created by stu on 2019/10/9.
//  Copyright © 2019年 stu. All rights reserved.
//

import UIKit

class TodoViewController: UITableViewController, AddItemViewControllerDelegate{
    
    var category: TodoCategory!
   
   
    
  
    required init?(coder aDecoder: NSCoder) {

        
        
        super.init(coder: aDecoder)
      
        
    }
  
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let category = category {
            title = category.name
        }

       
        
        
        
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return category.items.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TodolistItem", for: indexPath)
        
       let textLable = cell.viewWithTag(1000) as! UILabel
   
        let item = category.items[indexPath.row]
       
        
        textLable.text = item.text
        configCellCheckmark(for: cell, at: indexPath)
        
        
        return cell
    }
    func configCellCheckmark(for cell: UITableViewCell, at indexPath: IndexPath) {
        let checkLabel = cell.viewWithTag(1001) as! UILabel
       
               let item = category.items[indexPath.row]
        if item.checked {
           checkLabel.text = "√"
            
        }else {
            checkLabel.text = ""
        }
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
              return 50
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) {
           let item = category.items[indexPath.row]
            configCellCheckmark(for: cell, at: indexPath)
            item.checked = !item.checked
            tableView.deselectRow(at: indexPath, animated: true)
        }
      
    }
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        category.items.remove(at: indexPath.row)
        
        let paths = [indexPath]
        tableView.deleteRows(at: paths, with: .automatic)
        
    }
    func AddItemViewController(_ controller: AddItemViewController, endAdding item: TodoItem) {
        let newRow = category.items.count
        category.items.append(item)
        
        let indexPath = IndexPath(row: newRow, section: 0)
        let paths = [indexPath]
        tableView.insertRows(at: paths, with: .automatic)
     
        
    }
    func AddItemViewController(_ controller: AddItemViewController, endEditing item: TodoItem) {
        if let index = category.items.firstIndex(where: {$0 === item}) {
            category.items[index] = item
            
            let indexPath = IndexPath(row:index, section: 0)
            if let cell = tableView.cellForRow(at: indexPath) {
                let textLable = cell.viewWithTag(1000) as! UILabel
                textLable.text = item.text
            }
        }
        
        
    }
    
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "AddItem" {
        let dest = segue.destination as! UINavigationController
            let addItemViewController = dest.topViewController as! AddItemViewController
            addItemViewController.delegate = self
            
        }else if segue.identifier == "EditItem" {
            let dest = segue.destination as! UINavigationController
            let addItemViewController = dest.topViewController as! AddItemViewController
            addItemViewController.delegate = self
            if let index = tableView.indexPath(for: sender as! UITableViewCell) {
              addItemViewController.itemToEdit = category.items[index.row]
            }
        }
    }
    
}
